<template>
	<view class="w-full h-full">
		<!-- 语音 -->
		<view class="w-full h-full flex-col voicebg" style="position: relative">
			<view class="flex-col" style="color: #fff; font-weight: bold; line-height: 20px;z-index: 99;">
				<!-- <view class="w-full flex-col" style="padding: 16px 30px">
					<text style="font-size: 14px">高小博</text>
					<text style="font-size: 12px; font-weight: 400">高博会数字服务官</text>
				</view> -->
			</view>
			<!-- <view class="w-full h-full flex items-center" style="position: absolute;inset: 0;">
				<SZR SZR_class="voiceSZR" />
			</view> -->
			<view class="flex-col" style="margin-top: auto; z-index: 99999999">
				<view class="flex items-center" style="margin: 0 auto 14px">
					<RecorderButton></RecorderButton>
				</view>
				<view class="w-full h-full flex" style="
                        background: #fff;
                        height: 320px;
                        border-radius: 20px;
                        margin-top: auto;
                    ">
					<view class="flex-col w-full">
						<view class="flex" v-if="
                                conversationMessages &&
                                conversationMessages.length == 0
                            " style="padding: 12px 12px 0;">
							<image style="
                                    width: 36px;
                                    height: 36px;
                                    margin-right: 8px;
                                    min-width: 36px;
                                " src="../static/avatar.png" mode="">
							</image>
							<view class="w-full flex-col">
								<text style="
                                        font-weight: 400;
                                        font-size: 12px;
                                        color: #666666;
                                        line-height: 17px;
                                    ">{{titleObj.title}}</text>
								<view class="flex-col recommand" style="
                                        margin-top: 3px;
                                        width: 80%;
										
                                        height: auto;
                                        padding: 12px 16px;
                                    ">
									<view style="
                                            margin-bottom: 6px;
                                            font-weight: 500;
                                            font-size: 13px;
                                            color: #212121;
                                            line-height: 20px;
                                            padding-bottom: 6px;
                                            border-bottom: 1px solid #d8e9ff;
                                        ">
										您可以试着问我：</view>

									<view class="w-full flex-col" style="">
										<view class="flex items-center w-full" @click="fastsend(item.question)" :style="{
                                                marginBottom:
                                                    index + 1 == dataList.length
                                                        ? ''
                                                        : '8px',
                                            }" style="cursor: pointer" v-for="(
                                                item, index
                                            ) in displayedQuestions" :key="item.aid">
											<image style="
                                                    width: 18px;
                                                    height: 18px;
													min-width: 18px;
                                                " src="../static/q.png" mode="">
											</image>
											<text :title="item.question"
												style="
                                                    margin: 0 8px;
                                                    font-weight: 400;
                                                    font-size: 12px;
                                                    color: #212121;
                                                    line-height: 20px;
													white-space: nowrap;
													text-overflow: ellipsis;
													overflow: hidden;
                                                ">{{ item.question.length > 15 ? item.question.slice(0,15) : item.question }}</text>
											<image style="
                                                    width: 14px;
                                                    height: 14px;
                                                    margin-left: auto;
                                                " src="../static/fastsend.png" mode="">
											</image>
										</view>
									</view>

									<view @click="randomGet()" class="flex items-center" style="
                                            margin-top: auto;
                                            margin-left: auto;
                                            cursor: pointer;
                                        ">
										<image style="
                                                width: 14px;
                                                height: 14px;
                                                margin-right: 4px;
                                            " src="../static/refresh.png" mode="">
										</image>
										<text style="
                                                font-weight: 400;
                                                font-size: 12px;
                                                color: #1e43e9;
                                                line-height: 17px;
                                            ">换一批</text>
									</view>
								</view>
							</view>
						</view>

						<view class="hsc" style="
                                padding: 32px 12px;
                                height: calc(100% - 70px);
                                overflow-y: auto;
                            " v-else>
							<view class="flex-col" v-for="(item, index) in conversationMessages" :key="item.id">
								<view class="flex" style="margin: 0 0 16px 0">
									<image v-if="index + (1 % 2) == 0" style="
                                            width: 36px;
                                            height: 36px;
                                            margin-right: 8px;
                                            min-width: 36px;
                                        " src="../static/avatar.png" mode="">
									</image>
									<view style="
                                            margin-left: auto;
                                            background: linear-gradient(
                                                270deg,
                                                #458bfb 0%,
                                                #5f76fb 100%
                                            );
                                            border-radius: 8px 0px 8px 8px;
                                            padding: 8px 16px;
                                            max-width: 70%;
                                            color: #fff;
                                        ">
										<text style="
                                                font-weight: 400;
                                                font-size: 12px;
                                                line-height: 24px;
                                            ">{{ item.content }}</text>
									</view>
								</view>
								<view class="">
									<view class="flex" :style="{
                                            marginBottom:
                                                index + 1 ==
                                                conversationMessages.length
                                                    ? '40px'
                                                    : '16px',
                                        }" style="max-width: 100%">
										<image style="
                                                width: 36px;
                                                height: 36px;
                                                margin-right: 8px;
                                                min-width: 36px;
                                            " src="../static/avatar.png" mode="">
										</image>
										<view class="" style="
                                                background: #fafcff;
                                                border-radius: 12px;
                                                border: 1px solid #eceff3;
                                                padding: 8px 16px;
                                                max-width: 65%;
                                                width: fit-content;
                                            ">
											<view v-if="!item.answer" class="flex items-center text-center">
												<img src="../static/loading.png" style="
                                                        width: 24px;
                                                        height: 24px;
                                                        margin-right: 8px;
                                                    " class="rotateAnimate" alt="" />
												<span style="
                                                        font-size: 12px;
                                                        color: #333;
                                                    ">
													加载中...
												</span>
											</view>
											<view class="answerHtml" v-html="item.answer">
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
						<view class="flex-col" style="margin-top: auto;">
							<view class="hsc" style="
                                    overflow-x: auto;
                                    max-width: 90%;
                                    margin: 0px auto 10px;
                                ">
								<view class="flex items-center" style="margin-right: 20px">
									<text v-for="item in SuggestList" @click="(this.input = item), send()" style="
                                            cursor: pointer;
                                            font-size: 12px;
                                            color: #212121;
                                            margin-right: 10px;
                                            padding: 5px 10px;
                                            border: 1px solid #eee;
                                            border-radius: 36px;
                                            background: #fff;
                                            white-space: nowrap;
                                            box-shadow: 0px 0px 3px #eee;
                                        ">{{ item }}</text>
								</view>
							</view>
							<view class="flex items-center "
								style="position: relative; margin-top: auto;padding: 0 12px 0px;">
								<input @keydown="sendEnter($event)" class="w-full" v-model="input" style="
                                        background: #ffffff;
                                        border-radius: 22px;
                                        border: 1px solid #eceff3;
                                        font-size: 12px;
                                        min-height: 30px;
                                        padding: 6px 40px 6px 20px;
                                    " type="text" placeholder="很高兴为您服务,请描述您的问题" />
								<!-- <image style="width: 20px;cursor:pointer;height: 20px;position: absolute;right: 15px;"
					src="../../static/send.png" mode=""></image> -->

								<view @click="send" class="sendicon" style="
                                        position: absolute;
                                        right: 25px;
                                        width: 20px;
                                        cursor: pointer;
                                        height: 20px;
                                    ">
								</view>
							</view>
							<text style="margin: 5px auto 5px;color: #333;font-size: 10px;">pcVoice中犇教育科技技术支持</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import RecorderButton from "@/components/RecorderButton.vue";
	import SZR from "./szrnew.vue";
	import {
		useConversationStore
	} from "../store";
	import {
		member_id,
		ProBaseUrl
	} from "../common/config";
	const conversationStore = useConversationStore();
	export default {
		name: "pcVoice",
		data() {
			return {
				dataList: [],
				displayedQuestions: [],
				vocieInput: "",
				voiceDataList: [],
				input: "",
			};
		},
		components: {
			RecorderButton,
			SZR,
		},
		computed: {
			titleObj() {

				return {
					title: conversationStore.title,
					subtitle: conversationStore.subtitle
				}
			},
			SuggestList() {
				console.log(conversationStore.message_suggestedList);
				return conversationStore.message_suggestedList;
			},
			conversationMessages() {
				console.log(
					"conversationStore.voiceDataList",
					conversationStore.voiceMessages
				);
				const uniqueMessages = conversationStore.voiceMessages.reduce(
					(acc, current) => {
						const x = acc.find(item => item.id === current.id);
						if (!x) {
							return acc.concat([current]);
						} else {
							return acc;
						}
					},
					[]
				);
				console.log("conversationStore.voiceDataList", uniqueMessages);
				return uniqueMessages;
			},
		},

		mounted() {
			this.getConverstationList();
			this.$nextTick(() => {
				console.log(document.querySelector(".miniSZR .shuziren-chat"));
				document.querySelector(".shuziren-chat").style.display = "none";
			});
			this.bindImageClickEvents();
		},
		watch: {
			conversationMessages: {
				deep: true,
				handler() {
					this.$nextTick(() => {
						this.bindImageClickEvents();
					});
				},
			},
		},
		methods: {
			scrollBottom() {
				this.$nextTick(() => {
					const container = this.$el.querySelector('.hsc');
					if (container) {
						container.scrollTop = container.scrollHeight;
					}
				});
			},
			sendEnter(e) {
				console.log(e.code);
				if (e.code == "Enter") {
					this.send();
				}
			},
			bindImageClickEvents() {
				this.$nextTick(() => {
					const container = this.$el; // 当前组件的根元素
					const images = container.querySelectorAll(".answerHtml img");
					console.log(images);
					images.forEach(img => {
						// 避免重复绑定
						if (!img._listenerAdded) {
							img._listenerAdded = true;
							img.addEventListener("click", e => {
								const src = e.target.src;
								console.log("图片被点击:", src);
								this.handleImageClick(src);
							});
						}
					});
				});
			},
			handleImageClick(src) {
				// 自定义处理函数
				uni.previewImage({
					current: src,
					urls: [src],
				});
			},
			getConverstationList() {
				fetch(
						`${ProBaseUrl}/api/v1/681d77db9d7b3?member_id=${member_id}`
					)
					.then(res => res.json())
					.then(res1 => {
						if (res1.code == "1" && res1.data.length) {
							this.dataList = res1.data;
							this.randomGet();
						}
					});
			},
			randomGet() {
				if (this.dataList.length === 0) {
					this.displayedQuestions = [];
					return;
				}
				if (this.dataList.length <= 3) {
					this.displayedQuestions = [...this.dataList];
				} else {
					const shuffled = [...this.dataList].sort(
						() => 0.5 - Math.random()
					);
					this.displayedQuestions = shuffled.slice(0, 3);
				}
			},
			send() {
				console.log(123);
				if (!this.input || this.input.trim() === "") return;
				const conversationStore = useConversationStore();
				const id = new Date().getTime();
				// 如果正在说话，不发送消息
				// if (conversationStore.Speaking) return
				// conversationStore.ChangeSpeaking(true)
				conversationStore.sendMessage(this.input, id, "voice");

				this.$eventBus.$emit("sendMessage", {
					type: "H5_SZR_INSTRUCT",
					id,
					detail: {
						code: 3,
						value: this.input,
					},
					sendMessageType: "voice",
				});
				this.input = "";
				this.scrollBottom()
			},
			fastsend(item) {
				this.input = item;
				this.send();
			},
		},
	};
</script>

<style>
	/* .voicebg {
		border-radius: 16px;
		overflow: hidden;
	} */
	.answerHtml {
		font-weight: 400;
		font-size: 12px;
		color: #212121;
		word-break: break-all;
		line-height: 24px;
	}

	.answerHtml * {
		word-break: break-all;
	}

	.answerHtml img,
	.answerHtml uni-image {
		display: flex;
		width: 70% !important;
		margin: 5px auto;
	}

	.recommand {
		background: url("~@/static/recommand.png") center center / 100% 100% no-repeat;
	}

	.sendicon {
		background: url("~@/static/send.png") center center / 100% 100% no-repeat;
	}

	.sendicon:hover {
		background: url("~@/static/sendactive.png") center center / 100% 100% no-repeat;
	}

	@keyframes rotate {
		0% {
			transform: rotate(0deg);
		}

		50% {
			transform: rotate(180deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}

	.rotateAnimate {
		animation: rotate 1s linear infinite;
	}
</style>